<?php

namespace app\index\controller;

use PDO;
use think\Controller;
use think\response\Json;

class Index extends Controller
{
    public function design()
    {
        echo 'design';
    }
    
    public function index()
    {
        return 'hello think-php5.1';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function articles()
    {
        $articles = [];

        for ($i = 1; $i < 100; $i++) {
            $articles[] = [
                'id' => $i,
                'name' => '文章' . $i,
                'url' => '/articles/' . $i,
            ];
        }

        return Json::create($articles);
    }


    public function pdo()
    {
        $dsn = "mysql:host=localhost;dbname=llqhz";
        $db = new PDO($dsn, 'llqhz', 'llqhz');
        $count = $db->exec("INSERT INTO user SET name = 'llqhz'");

        $stmt = $db->prepare('update user set name = "?" where id = ?');
        $result = $stmt->execute(['llqhz', 2]);

        var_dump($result);

        echo $count;
        $db = null;
    }

    public function info()
    {
        phpinfo();
    }

    public function swoole()
    {
        return \json($this->request);
    }


    public function read()
    {
        $name = 'llqhz';

        $dsn = 'mysql:host=localhost;dbname=llqhz';
        $db = new PDO($dsn, 'llqhz', 'llqhz');

        $stmt = $db->query('select * from user where name = :name');
        $stmt->bindParam(':name', $name, PDO::PARAM_STR_CHAR);

        $result = $stmt->fetchAll(PDO::FETCH_CLASS);
        foreach($result as $row) {
            $row["name"] = strval($row["name"]);
        }

        return $result;
    }

}
